package cn.kgc.ssm.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.web.entity.User;
import cn.kgc.ssm.web.form.LoginForm;
import cn.kgc.ssm.web.mapper.UserMapper;
import cn.kgc.ssm.web.response.ResponseResult;
import cn.kgc.ssm.web.service.UserService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/7/26
 * @description
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public ResponseResult login(LoginForm loginForm, HttpSession session) {

        // 1.  比较验证码是否正确
        String code = loginForm.getCode();
        String sessionCode = (String) session.getAttribute("code");

        if (!sessionCode.equals(code)) {
            return ResponseResult.fail().message("验证码错误");
        }

        // 2. 校验用户名密码      123456   ->  MD5
        String password = loginForm.getPassword();
        String MD5Password = SecureUtil.md5(password);
        loginForm.setPassword(MD5Password);

        User user = userMapper.selectUser(loginForm);
        if (ObjectUtils.isEmpty(user)) {
            return ResponseResult.fail().message("用户名或密码错误");
        }

        // 用户登录成功   保存用登录状态
        session.setAttribute("user", user);
        return ResponseResult.success().message("认证通过");
    }

    @Override
    public ResponseResult userPage(Integer page, Integer limit, String username, Integer age) {

        PageHelper.startPage(page, limit);
        //  如何实现 搜索和查询所有共用一个接口
        List<User> users = userMapper.userPage(username, age);
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        // 数据总条数
        long total = userPageInfo.getTotal();

        return ResponseResult.success().data(users).count(total);
    }

    @Override
    public ResponseResult addUser(User user) {

        // 查询用户名是否存在
        User usr = userMapper.selectByUsername(user.getUsername());
        if (ObjectUtil.isNotEmpty(usr)){
            return ResponseResult.fail().message("用户名已存在");
        }

        // 设置默认密码
        String defaultPassword = "111111";
        user.setPassword(SecureUtil.md5(defaultPassword));

        userMapper.addUser(user);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult editUser(User user) {

        userMapper.updateUser(user);

        return ResponseResult.success();
    }


    //   控制批量删除的事务
    @Override
    @Transactional
    public ResponseResult batchDelete(Integer[] ids) {

        userMapper.delete(ids);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult resetPassword(Integer id) {

        String defaultPassword = "111111";
        String md5Pwd = SecureUtil.md5(defaultPassword);

        userMapper.resetPwd(id,md5Pwd);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult getPersonInfo(String username) {

        User user = userMapper.selectByUsername(username);

        return ResponseResult.success().data(user);
    }

    @Override
    public ResponseResult editPersonalPassword(String oldPassword, String newPassword, HttpSession session) {

        //  1. 判定旧密码是否正确
        User user = (User) session.getAttribute("user");
        User usr = userMapper.selectByUsername(user.getUsername());
        String DBPassword = user.getPassword();  //  MD5
        String MD5FormPassword = SecureUtil.md5(oldPassword);

        if (!StringUtils.equals(DBPassword,MD5FormPassword)){
            return ResponseResult.fail().message("原始密码不正确");
        }

        // 更新密码
        String MD5NewPassword = SecureUtil.md5(newPassword);
        Integer uid = usr.getId();
        userMapper.resetPwd(uid,MD5NewPassword);

        return ResponseResult.success();
    }
}
